<template>
  <main class="details">
    <section class="details-con" v-if="goodsList.length > 0">
      <div class="con-img">
        <img :src="host + (goodsList[0] ? goodsList[0].image : '')" alt="" />
      </div>
      <div class="con-text">
        <h4>{{ goodsList[0] ? goodsList[0].name : "" }}</h4>
        <p>分类：{{ goodsList[0] ? goodsList[0].type_name : "" }}</p>
        <p>
          {{ goodsList[0] ? goodsList[0].intro : "" }}
        </p>
        <div>
          <p>
            数量:{{ goodsList[0] ? goodsList[0].stock : "" }}/销量:{{
              goodsList[0] ? goodsList[0].sales : ""
            }}
          </p>
          <p>￥{{ goodsList[0] ? goodsList[0].price : "" }}</p>

          <a
            href="javascript:;"
            @click="addToCart(goodsList[0] ? goodsList[0].id : 0)"
            >加入购物车</a
          >
          <a
            href="javascript:;"
            @click="
              toPay({
                amount: 1,
                goods_id: goodsList[0].id,
                id: goodsList[0].id,
                image: goodsList[0].image,
                name: goodsList[0].name,
                price: goodsList[0].price,
              })
            "
          >
            立即购买
          </a>
        </div>
      </div>
    </section>
  </main>
</template>

<script>
import { mapState } from "vuex";
import config from "../../config";
import { addToCartApi } from "../../api";
export default {
  name: "Details",
  data() {
    return {
      host: config.host,
    };
  },
  computed: {
    ...mapState("goods", ["goodsList"]),
  },
  methods: {
    // 加入购物车
    addToCart(goods_id) {
      addToCartApi(
        (response) => {
          if (response.data.status === 1) {
            // 提示信息
            this.$bus.$emit("showTips", "添加购物车成功！");
            // 重新获取购物车长度
            this.$store.dispatch("user/getCartLen");
          }
        },
        (err) => {
          console.log(err);
        },
        { goods_id, amount: 1 }
      );
    },
    // 下单
    toPay(data) {
      this.addToCart(data.id);
      this.$store.commit("pay/ADDLIST", [data]);
      this.$router.push("/pay");
    },
  },
  mounted() {
    this.$store.dispatch("goods/getById", { id: this.$route.query.id });
  },
};
</script>

<style lang="stylus" scoped>
.details {
  width: 100%;
  min-width: 800px;
  min-height: 400px;
  margin-top: 60px;
  background-color: #f8f8f8;

  .details-con {
    width: 80%;
    min-width: 800px;
    margin: 0 auto;
    padding: 40px 0 40px;
    display: flex;
    justify-content: space-between;
  }

  .con-img {
    width: 40%;

    img {
      width: 100%;
    }
  }

  .con-text {
    width: 54%;
    position: relative;

    h4 {
      font-size: 30px;
      line-height: 50px;
    }

    &>p {
      font-size: 16px;
      // max-height: 110px;
      overflow: auto;
      color: #999;
      margin: 20px 0;
    }

    &>p:last-of-type {
      height: 34%;
    }

    div {
      position: absolute;
      bottom: 20px;
      left: 0;

      p {
        font-size: 14px;
        color: #fe4d1b;
      }

      p:last-of-type {
        // color: #fe4d1b;
        padding-bottom: 12px;
      }

      a {
        padding: 6px 20px;
        margin-right: 20px;
        text-decoration: none;
        color: #fff;
        background-color: #fe4d1b;
      }
    }
  }
}
</style>